package com.reebake.ideal.message.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.reebake.ideal.message.entity.MessageVerifyCodeEntity;
import com.reebake.ideal.message.service.MessageVerifyCodeService;
import com.reebake.ideal.message.vo.VerifyCodePredictVO;
import com.reebake.ideal.message.vo.VerifyCodeSendVO;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/message/verify-code")
public class MessageVerifyCodeController {
    private final MessageVerifyCodeService messageVerifyCodeService;

    @PostMapping("/send")
    public void verifyCode(@RequestBody @Valid VerifyCodeSendVO verifyCodeSendVO) {
        messageVerifyCodeService.sendVerifyCode(BeanUtil.copyProperties(verifyCodeSendVO, MessageVerifyCodeEntity.class));
    }

    @PostMapping("/predict")
    public VerifyCodePredictVO predict(@RequestBody @Valid VerifyCodePredictVO verifyCodePredictVO) {
        String username = verifyCodePredictVO.getUsername();
        VerifyCodePredictVO r = new VerifyCodePredictVO();
        if(StrUtil.isBlank(username)) {
            return r;
        }
        r.setRequireCaptcha(messageVerifyCodeService.requireCaptcha(username));

        return r;
    }

}
